#!/bin/bash
#
###############################################################################
#                                                                             #
#                      Pi-Star Firmware Update Tool                           #
#                                                                             #
#    Version 1.1, Code, Design and Development by Andy Taylor (MW0MWZ).       #
#                                                                             #
#                 Make it simple to update the Firmware                       #
#                                                                             #
###############################################################################
#
if [ "$(id -u)" != "0" ]; then
  echo -e "You need to be root to run this command...\n"
  exit 1
fi
#
# Output some usefull information when no variables are passed
#
if [ -z "$1" ]
then
  echo "This tool is used to flash your ZUMSpot Modem"
  echo "to use the command, you will need to pass the name of your modem,"
  echo "please choose form the list below..."
  echo ""
  echo "To flash a ZUMSpot connected to GPIO, run:         pistar-zumspotflash rpi"
  echo "To flash a ZUMSpot duplex connected to GPIO, run:  pistar-zumspotflash rpi_duplex"
  echo "To flash a ZUMSpot USB Key, run:                   pistar-zumspotflash usb"
  echo "To flash a USB Connected Libre Modem, run:         pistar-zumspotflash libre"
  echo ""
  echo "To flash MMDVM_HS Hat devices by DB9MAT & DF2ET please use pistar-mmdvmhshatflash"
  echo "To flash MMDVM_HS Nano Hat devices please use pistar-vyehsflash"
  echo "To flash your BD7KLE/BG3MDO devices please use pistar-mdoflash"
  echo ""
  # Get the pretty name for the chosen Modem according to the dashboard.
  if [ -f '/etc/dstar-radio.mmdvmhost' ]; then
    # Get the modem that the user has selected.
    modemSelected=$(grep 'Hardware=' /etc/dstar-radio.mmdvmhost)
    modemSelected=${modemSelected#*=}

    # Get the user friendly version of the selected Modem.
    modemSelectedHuman=$(grep ${modemSelected} /var/www/dashboard/admin/configure.php)
    modemSelectedHuman=${modemSelectedHuman#*\">}
    modemSelectedHuman=${modemSelectedHuman::-9}
    echo "Selected Modem:     ${modemSelectedHuman}"
  fi

  # Get the physically connected modem from the logs
  numLogs=$(ls -1rt /var/log/pi-star/MMDVM-*.log | wc -l)
  if [[ ${numLogs} -gt 0 ]]; then
    modemLog=$(tac $(ls -1rt /var/log/pi-star/MMDVM-*.log | tail -n1 ) | grep "protocol" -m 1 | sed -n 's/description: /&\n/;s/.*\n//p')
    echo "Connected Hardware: ${modemLog}"
  fi
  exit 0
fi
#
# OK we know what the modem is, let get the firmware string from the log...
#
tac $(ls -1rt /var/log/pi-star/MMDVM-*.log | tail -n1 ) | grep "protocol" -m 1 | sed -n 's/description: /&\n/;s/.*\n//p'
read -n 1 -s -r -p "Press any key to write the ${1} firmware to this modem or Ctrl-C to quit..."
echo ""
#
# Download the correct firmware
#
if [ "$1" ]
then
  VERSION=$(curl -Ls -o /dev/null -w %{url_effective} https://github.com/juribeparada/MMDVM_HS/releases/latest | awk -F "/v" '{print $2}')
  case ${1} in
  rpi)
    # Flashing RPi GPIO Modem
    rm -rf /tmp/zumfirmware 2> /dev/null
    mkdir /tmp/zumfirmware 2> /dev/null
    echo "Flashing your ${1} modem to the latest version"
    wget https://github.com/juribeparada/MMDVM_HS/releases/download/v${VERSION}/install_fw_rpi.sh -O /tmp/zumfirmware/flash.sh
    chmod +x /tmp/zumfirmware/flash.sh
    cd  /tmp/zumfirmware
    ./flash.sh
    echo ""
    rm -rf /tmp/zumfirmware 2> /dev/null
    read -n 1 -s -r -p "Flashing your ${1} modem complete, press any key to reboot your Pi-Star System..."
    shutdown -r now
    exit 0
  ;;
  rpi_duplex)
    # Flashing RPi Duplex GPIO Modem
    rm -rf /tmp/zumfirmware 2> /dev/null
    mkdir /tmp/zumfirmware 2> /dev/null
    echo "Flashing your ${1} modem to the latest version"
    wget https://github.com/juribeparada/MMDVM_HS/releases/download/v${VERSION}/install_fw_duplex.sh -O /tmp/zumfirmware/flash.sh
    chmod +x /tmp/zumfirmware/flash.sh
    cd  /tmp/zumfirmware
    ./flash.sh
    echo ""
    rm -rf /tmp/zumfirmware 2> /dev/null
    read -n 1 -s -r -p "Flashing your ${1} modem complete, press any key to reboot your Pi-Star System..."
    shutdown -r now
    exit 0
  ;;
  usb)
    # Flashing USB Modem
    rm -rf /tmp/zumfirmware 2> /dev/null
    mkdir /tmp/zumfirmware 2> /dev/null
    echo "Flashing your ${1} modem to the latest version"
    wget https://github.com/juribeparada/MMDVM_HS/releases/download/v${VERSION}/install_fw_usb.sh -O /tmp/zumfirmware/flash.sh
    chmod +x /tmp/zumfirmware/flash.sh
    cd  /tmp/zumfirmware
    ./flash.sh
    echo ""
    rm -rf /tmp/zumfirmware 2> /dev/null
    read -n 1 -s -r -p "Flashing your ${1} modem complete, press any key to reboot your Pi-Star System..."
    shutdown -r now
    exit 0
  ;;
  libre)
    # Flashing Libre Kit Modem
    rm -rf /tmp/zumfirmware 2> /dev/null
    mkdir /tmp/zumfirmware 2> /dev/null
    echo "Flashing your ${1} modem to the latest version"
    wget https://github.com/juribeparada/MMDVM_HS/releases/download/v${VERSION}/install_fw_librekit.sh -O /tmp/zumfirmware/flash.sh
    chmod +x /tmp/zumfirmware/flash.sh
    cd  /tmp/zumfirmware
    ./flash.sh
    echo ""
    rm -rf /tmp/zumfirmware 2> /dev/null
    read -n 1 -s -r -p "Flashing your ${1} modem complete, press any key to reboot your Pi-Star System..."
    shutdown -r now
    exit 0
  ;;
  *)
    # Un-recognised modem choice
    echo "Your Choice of ${1} modem is not supported by this update tool"
    exit 1
  ;;
  esac
fi
exit 0
